// pages/discount/ClassCoupon.js
var Zan = require('../../dist/index.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
let WxNotificationCenter = require('../../utils/WxNotificationCenter.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start01 = 0;
var start02 = 0;
var start03 = 0;
let constants = null;
Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    types:'',
    typeId:'',
    lat:'',
    lng: '',
    province: '',
    city: '',
    district: '',
    title: null,
    classCouponList01: [],
    classCouponList02: [],
    classCouponList03: [],
    nodata: {
      msg: '暂无数据',
      notype: 1,
      imgurl: '/images/nodatas.png',
    }
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    this.setData({
      typeId: options.id
    })
    wx.setNavigationBarTitle({
      title: decodeURIComponent(options.name),
    })
    console.log(options.name);

    constants = new app.Constants();
    wx.showLoading({
      title: '',
    })
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        let latitude = res.latitude
        let longitude = res.longitude
        wx.request({
          url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${constants.key}`,
          success: res => {
            wx.hideLoading();
            let location = res.data.result.ad_info;
            that.setData({
              lng: res.data.result.location.lng,
              lat: res.data.result.location.lat,
              province: location.province,
              city: location.city,
              district: location.district
              // region: [location.province, location.city, location.district]
            })
            that.onPullDownRefresh()
          }
        })

      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   * 
   * typeId
   * lat,
   * lng,
   * types,
   * sheng,shi,qu,
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    if (this.data.activeIndex == 0){
      start01 = 0;
      this.setData({
        classCouponList01: []
      })
      network._post_page_json(this, api.tTypeCouponList, start01, { typeId: this.data.typeId, types: 1, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess01);
    } else if (this.data.activeIndex == 1){
      start02 = 0;
      this.setData({
        classCouponList02: []
      })
      network._post_page_json(this, api.tTypeCouponList, start02, { typeId: this.data.typeId, types: 2, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess02);
    }else{
      start03 = 0;
      this.setData({
        classCouponList03: []
      })
      network._post_page_json(this, api.tTypeCouponList, start03, { typeId: this.data.typeId, types: 3, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess03);
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.activeIndex == 0) {
      if (this.data.classCouponList01.length < this.data.recordsTotal) {
        start01 = start01 + 20;
        network._post_page_json(this, api.tTypeCouponList, start01, { typeId: this.data.typeId, types: 1, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess01);
      }
    } else if (this.data.activeIndex == 1) {
      if (this.data.classCouponList02.length < this.data.recordsTotal) {
        start02 = start02 + 20;
        network._post_page_json(this, api.tTypeCouponList, start02, { typeId: this.data.typeId, types: 2, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess02);
      }
    } else {
      if (this.data.classCouponList03.length < this.data.recordsTotal) {
        start03 = start03 + 20;
        network._post_page_json(this, api.tTypeCouponList, start03, { typeId: this.data.typeId, types: 3, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess03);
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  goCouponDetails: function(e){
    var id = e.currentTarget.dataset.id;
    // console.log(id);
    wx.navigateTo({
      url: '/pages/discount/CouponDetails' + '?couponId=' + id,
    })
  },
  /**
   * 点击tab
   */
  selected :function(e){
    let index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      activeIndex: index
    })
    if(index == 0){
      start01 = 0;
      this.setData({
        classCouponList01:[]
      });
      network._post_page_json(this, api.tTypeCouponList, start01, { typeId: this.data.typeId, types: 1, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess01);
    }else if(index == 1){
      start02 = 0;
      this.setData({
        classCouponList02: []
      });
      network._post_page_json(this, api.tTypeCouponList, start02, { typeId: this.data.typeId, types: 2, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess02);
    }else{
      start03 = 0;
      this.setData({
        classCouponList03: []
      });
      network._post_page_json(this, api.tTypeCouponList, start03, { typeId: this.data.typeId, types: 3, lat: this.data.lat, lng: this.data.lng, province: this.data.province, city: this.data.city, district: this.data.district }, classSuccess03);
    }


  }
}))
var classSuccess01 = function(that,res){
  console.log(res);
  if(res.data.data){
    that.setData({
      classCouponList01: that.data.classCouponList01.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
    
  }
}
var classSuccess02 = function (that, res) {
  console.log(res);
  if (res.data.data) {
    that.setData({
      classCouponList02: that.data.classCouponList02.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
    
  }
}
var classSuccess03 = function (that, res) {
  console.log(res);
  if (res.data.data) {
    that.setData({
      classCouponList03: that.data.classCouponList03.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
    
  }
}